package com.qianguo.sender.tab;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.sender.R;
import com.qianguo.sender.api.entity.CatchOrder;
import com.qianguo.sender.api.entity.SenderOrderInfo;
import com.qianguo.sender.api.entity.SenderOrderList;
import com.qianguo.sender.base.BaseFragment;
import com.qianguo.sender.order.SenderOrderDetailActivity;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 派送员抢单页面
 *
 * @author eva
 * @date 16/4/29
 */
@EFragment(R.layout.layout_swipe_list_view)
public class SenderCatchOrderFragment extends BaseFragment {
    @ViewById(R.id.swipe_refresh_recycler_view)
    SwipeRefreshRecyclerView mRecyclerList;

    private CatchOrderAdapter mAdapter;
    private List<SenderOrderInfo> mTodoOrders = new ArrayList<>();

    private int mCurrentPage = 1;
    private static final int PER_PAGE = 10;

    @Override
    protected void initializeUI() {

        mAdapter = new CatchOrderAdapter(getActivity());
        mRecyclerList.setAdapter(mAdapter);
        mRecyclerList.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onLoadMore() {
                mCurrentPage++;
                getTodoOrders(mCurrentPage);
            }

            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mTodoOrders.clear();
                getTodoOrders(mCurrentPage);
            }
        });

        mRecyclerList.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                SenderOrderDetailActivity.jumpToOrderDetail(getActivity(), mTodoOrders.get(position).getOrdersn());
            }
        });

        getTodoOrders(mCurrentPage);
    }

    /**
     * 获取待接单订单列表
     */
    @Background
    void getTodoOrders(int page) {
        mApp.getApi().catchOrders(mApp.getUser().getToken(), page, PER_PAGE, new HttpRequestCallback<SenderOrderList>() {
            @Override
            public void onSuccess(int code, String msg, SenderOrderList data) {
                mTodoOrders.addAll(data.getList());
                refreshOrders();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);

            }
        });

    }

    @Background
    void catchOrder(String ordersn) {
        mApp.getApi().catchOrder(mApp.getUser().getToken(), ordersn, new HttpRequestCallback<CatchOrder>() {
            @Override
            public void onSuccess(int code, String msg, CatchOrder data) {
                showToast(getActivity(), "抢单成功");
                mCurrentPage = 1;
                mTodoOrders.clear();
                getTodoOrders(mCurrentPage);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    /**
     * 刷新列表
     */
    @UiThread
    void refreshOrders() {
        mRecyclerList.onCompleted();
        mRecyclerList.notifyDataSetChanged();
    }

    private class CatchOrderAdapter extends RecyclerView.Adapter<CatchOrderViewHolder> {

        private Context mContext;
        public CatchOrderAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public CatchOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_order_list, parent, false);
            return new CatchOrderViewHolder(v);
        }

        @Override
        public void onBindViewHolder(CatchOrderViewHolder holder, int position) {
            holder.onBind(mTodoOrders.get(position));
        }

        @Override
        public int getItemCount() {
            return mTodoOrders != null ? mTodoOrders.size() : 0;
        }
    }

    private class CatchOrderViewHolder extends RecyclerView.ViewHolder {
        TextView tvConsignee;
        TextView tvReceivingTime;
        TextView tvReceivingAddress;
        TextView tvShopAddress;
        TextView tvDeliveryDistance;
        TextView tvDeliveryPrice;
        TextView tvIM;
        Button btnCatch;

        private SenderOrderInfo item;

        public CatchOrderViewHolder(final View itemView) {
            super(itemView);
            tvConsignee = (TextView) itemView.findViewById(R.id.item_order_list_consignee_text_view);
            tvReceivingTime = (TextView) itemView.findViewById(R.id.item_order_list_receiving_time_text_view);
            tvReceivingAddress = (TextView) itemView.findViewById(R.id.item_order_list_receiving_address_text_view);
            tvShopAddress = (TextView) itemView.findViewById(R.id.item_order_list_shop_address_text_view);
            tvDeliveryDistance = (TextView) itemView.findViewById(R.id.item_order_list_delivery_distance_text_view);
            tvDeliveryPrice = (TextView) itemView.findViewById(R.id.item_order_list_delivery_price_text_view);
            // TODO: 16/5/5 IM
            btnCatch = (Button) itemView.findViewById(R.id.item_order_list_action_button);
            btnCatch.setText(getString(R.string.sender_order_action_catch_order));
            btnCatch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    catchOrder(item.getOrdersn());
                }
            });
        }


        void onBind(SenderOrderInfo order) {
            this.item = order;
            tvConsignee.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_consignee), order.getReceiverName()));
            tvReceivingAddress.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_receiving_address), order.getReceiverAddress()));
            tvReceivingTime.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_receiving_time), order.getDeliveryTime()));
            tvShopAddress.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_shop_address), order.getStoreAddress()));
            tvDeliveryDistance.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_delivery_distance), order.getDeliveryDistance()));
            tvDeliveryPrice.setText(getString(R.string.sender_key_value_template,
                    getString(R.string.sender_order_delivery_price), Integer.valueOf(order.getDeliveryPrice())));
        }
    }
}
